class ChartManager {
    constructor() {
        this.chart = null;
        this.initChart();
    }

    initChart() {
        const ctx = document.getElementById('moistureChart').getContext('2d');
        this.chart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: [],
                datasets: [{
                    label: '土壤墒情历史数据',
                    data: [],
                    borderColor: '#4CAF50',
                    tension: 0.1
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        max: 100
                    }
                }
            }
        });
    }

    updateChart(value) {
        const now = new Date();
        const timeStr = now.getHours() + ':' + now.getMinutes();
        
        this.chart.data.labels.push(timeStr);
        this.chart.data.datasets[0].data.push(value);

        // 保持最近12个数据点
        if (this.chart.data.labels.length > 12) {
            this.chart.data.labels.shift();
            this.chart.data.datasets[0].data.shift();
        }

        this.chart.update();
    }
} 